

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package coordinates;


import static com.googlecode.javacv.cpp.opencv_imgproc.cvGetCentralMoment;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvGetSpatialMoment;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvMoments;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_imgproc.CvMoments;

/**
 *
 * @author Shashindra
 */
public class MeanDetector {

int posX,posY;

    //given a BW image these two methods return the center of gravity posX,posY
    public int getMeanX(IplImage img){
        calculate(img);
        return posX;
    }

    public int getMeanY(IplImage img){
        calculate(img);
        return posY;
    }

    private void calculate(IplImage img){
        CvMoments moments = new CvMoments();
        cvMoments(img, moments, 1);

        double mom10 = cvGetSpatialMoment(moments, 1, 0);
        double mom01 = cvGetSpatialMoment(moments, 0, 1);
        double area = cvGetCentralMoment(moments, 0, 0);
        posX = (int) (mom10 / area);
        posY = (int) (mom01 / area);

    }
}
